package org.alanlau.consumer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 死信队列消费者
 *
 * @author alan
 * @version 1.0
 * @date 2021/4/8 21:17
 */
@Component
public class DelayMessageCustomer {
    @RabbitListener(queues = "new.queue")
    public void receiveMessage(String content, Message message, Channel channel) {
        System.out.println("来到了死信队列，正在消费中...");
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        String messageId = message.getMessageProperties().getMessageId();

        System.out.println("消息投递ID → ：" + deliveryTag);
        System.out.println("消息自定义ID → ：" + messageId);

        try {
            channel.basicAck(deliveryTag, false);

            System.out.println("签收成功：" + content);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("签收成功时间为：" + sdf.format(new Date()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
